//
//  BannerView.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/7.
//

#import "BannerRepayView.h"
#import "HomeModel.h"

@interface BannerRepayView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSTimer *timer;


@end

@implementation BannerRepayView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setBannerList:(NSArray *)bannerList{
   
    _bannerList = bannerList;
   
    [self.timer invalidate];
    
    [self setUpUI];
    
}


- (void)setUpUI{
    
    if (self.scroll) {
        [self.scroll removeFromSuperview];
    } 
    
    CGFloat width = self.width;
    
    CGFloat height = self.height;
    
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
    [self addSubview:self.scroll];
    

    NSInteger count = self.bannerList.count;
    
    if (count <= 1) {
        
        HomeBannerModel *banner = [HomeBannerModel mj_objectWithKeyValues:self.bannerList.firstObject];
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = [UIColor whiteColor];
    
        UILabel *lab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:14] color:kButtonRedColor];
        [imageView addSubview:lab];
        lab.numberOfLines = 0;
        lab.frame = CGRectMake(10, 0, width - 10 - 80 - 10, height);
        lab.text = esString(banner.ego);

        UIImage *imgB = [UIImage imageNamed:@"icon_btnBv2"];
        UIButton *repayBtn = [[UIButton alloc] init];
        [repayBtn setBackgroundImage:imgB forState:UIControlStateNormal];
        repayBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
        [repayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [imageView addSubview:repayBtn];
        [repayBtn setTitle:@"Repayment" forState:UIControlStateNormal];
        [repayBtn setTitle:@"Repayment" forState:UIControlStateSelected];
        [repayBtn setTitle:@"Repayment" forState:UIControlStateHighlighted];
        repayBtn.frame = CGRectMake(width - 80, 0, 80, 30);
        repayBtn.centerY = lab.centerY;
        
        repayBtn.tag = 0;
        [repayBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addTapForImageView:imageView];
       
        [self.scroll addSubview:imageView];
       
        return;
    }
    
    for (int i = 0; i < count + 2; i++) {
        CGFloat x = i*width;
        CGFloat y = 0;
        
        HomeBannerModel * banner = nil;
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        
        UIButton *repayBtn = [[UIButton alloc] init];
        
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = [UIColor whiteColor];
        
        if (i == 0) {
            banner = [HomeBannerModel mj_objectWithKeyValues:self.bannerList[count - 1]];
            imageView.tag = count - 1;
            repayBtn.tag = count - 1;
        } else if (i == count + 1) {
            banner = [HomeBannerModel mj_objectWithKeyValues:self.bannerList[0]];
            imageView.tag = 0;
            repayBtn.tag = 0;
        } else {
            banner = [HomeBannerModel mj_objectWithKeyValues:self.bannerList[i - 1]];
            imageView.tag = i - 1;
            repayBtn.tag = i - 1;
        }
        
        UILabel *lab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:14] color:kButtonRedColor];
        [imageView addSubview:lab];
        lab.numberOfLines = 0;
        lab.frame = CGRectMake(10, 0, width - 10 - 80 - 10, height);
        lab.text = esString(banner.ego);
        
        
        
        UIImage *imgB = [UIImage imageNamed:@"icon_btnBv2"];
        
        [repayBtn setBackgroundImage:imgB forState:UIControlStateNormal];
        repayBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
        [repayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [imageView addSubview:repayBtn];
        [repayBtn setTitle:@"Repayment" forState:UIControlStateNormal];
        [repayBtn setTitle:@"Repayment" forState:UIControlStateSelected];
        [repayBtn setTitle:@"Repayment" forState:UIControlStateHighlighted];
        repayBtn.frame = CGRectMake(width - 80, 0, 80, 30);
        repayBtn.centerY = lab.centerY;
        [repayBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addTapForImageView:imageView];
        [self.scroll addSubview:imageView];
        
        
    }
    

    self.scroll.delegate = self;
    CGPoint contentOffset = CGPointMake(width, 0);
    self.scroll.contentOffset = contentOffset;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.bounces = NO;
    self.scroll.pagingEnabled = YES;
    

    self.scroll.contentSize = CGSizeMake(width*(self.bannerList.count + 2), height);
    

    self.page = [[UIPageControl alloc] init];
    self.page.numberOfPages = self.bannerList.count;
    [self.page setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
    
    if (self.bannerList.count > 1) {
//        [self addSubview:self.page];
    }
    
    [self startTimer];
}

- (void)addTapForImageView:(UIImageView *)imageView{
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [imageView addGestureRecognizer:tap];
}

- (void)tap:(UIGestureRecognizer *)recognizer{
    UIImageView * imageView = (UIImageView *)recognizer.view;
    HomeBannerModel * banner = [HomeBannerModel mj_objectWithKeyValues:self.bannerList[imageView.tag]];
    if (_getBannerUrlString) {
        _getBannerUrlString(banner.exulting);
    }     
}

- (void)clickBtn:(UIButton *)btn{
    HomeBannerModel * banner = [HomeBannerModel mj_objectWithKeyValues:self.bannerList[btn.tag]];
    if (_getBannerUrlString) {
        _getBannerUrlString(esString(banner.exulting));
    }
}


- (void)startTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
}


- (void)autoScroll{

    NSInteger page = self.page.currentPage;
    page++;

    page = page >= self.bannerList.count ? 0 : page ;
    self.page.currentPage = page;

    [self turnPage];
}


- (void)turnPage{

    CGFloat width = self.scroll.frame.size.width;
    CGFloat height = self.scroll.frame.size.height;
    
    kWeakSelf
    __block CGPoint contentOffset = self.scroll.contentOffset;
    if (self.page.currentPage) {
        [UIView animateWithDuration:0.25f animations:^{
            contentOffset.x = width * (weakSelf.page.currentPage + 1);
            [weakSelf.scroll setContentOffset:contentOffset];
        }];
    } else {

        [UIView animateWithDuration:0.25f animations:^{
            contentOffset.x = width * (weakSelf.bannerList.count + 1);
            [weakSelf.scroll setContentOffset:contentOffset];
        } completion:^(BOOL finished) {
            [weakSelf.scroll scrollRectToVisible:CGRectMake(width, 0, width, height) animated:NO];
        }];
    }
}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self startTimer];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat width = self.scroll.frame.size.width;
    int page = self.scroll.contentOffset.x / width;
    page = (page == (self.bannerList.count + 1) ? 0 : page - 1);
    self.page.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat width = self.scroll.frame.size.width;
    CGFloat height = self.scroll.frame.size.height;
    int currentPage = self.scroll.contentOffset.x / width;
    if (currentPage == 0) {

        [self.scroll scrollRectToVisible:CGRectMake(width * self.bannerList.count, 0, width, height) animated:NO];
    } else if (currentPage == (self.bannerList.count + 1)) {

        [self.scroll scrollRectToVisible:CGRectMake(width, 0, width, height) animated:NO];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
